package com.water.camera.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.App;
import com.water.camera.adapter.PointAdapter;
import com.water.camera.databinding.DialogEditPointBinding;
import com.water.camera.util.ToastUtil;
import com.water.camera.util.Utils;

public class EditPointDlg extends FullScreenDlg {

    DialogEditPointBinding binding;
    PointAdapter adapter;
    String key;

    public EditPointDlg now(String key) {
        this.key = key;
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogEditPointBinding.inflate(inflater, container, false);
        binding.rvHistory.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.ivClose.setOnClickListener(v -> dismiss());

        float lat = SPHelper.getInstance(App.getApp()).getFloat(key + "_lat", -1);
        float lng = SPHelper.getInstance(App.getApp()).getFloat(key + "_lng", -1);
        if (lat >= 0) {
            binding.etContent1.setText(lat + "");
        }
        if (lng >= 0) {
            binding.etContent.setText(lng + "");
        }
        int now = SPHelper.getInstance(App.getApp()).getInt(key + "_point", 0);
        adapter = new PointAdapter(this, now);
        binding.rvHistory.setAdapter(adapter);

        binding.btnSave.setOnClickListener(v -> {
            save();
        });
        return binding.getRoot();
    }

    private void save() {
        float lat, lng;
        try {
            lng = Double.valueOf(binding.etContent.getText().toString().trim()).floatValue();//经
            if (lng < 0) {
                ToastUtil.showShortToast(getActivity(), "请输入正确的经度");
                return;
            }
        } catch (Throwable e) {
            ToastUtil.showShortToast(getActivity(), "请输入经度");
            return;
        }
        try {
            lat = Double.valueOf(binding.etContent1.getText().toString().trim()).floatValue();//纬
            if (lat < 0) {
                ToastUtil.showShortToast(getActivity(), "请输入正确的纬度");
                return;
            }
        } catch (Throwable e) {
            ToastUtil.showShortToast(getActivity(), "请输入纬度");
            return;
        }
        SPHelper.getInstance(App.getApp()).putFloat(key + "_lat", lat);
        SPHelper.getInstance(App.getApp()).putFloat(key + "_lng", lng);
        SPHelper.getInstance(App.getApp()).putInt(key + "_point", adapter.getSelect()).commit();
        dismiss(true, Utils.currentPatternContent(lat, lng, Utils.PATTERN.get(adapter.getSelect())));
    }

    @Override
    protected int getHeight() {
        getDialog().getWindow().setGravity(Gravity.BOTTOM);
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }
}
